﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RentAWorker.Business.Interfaces;
using RentAWorker.Business.Impl.Util;
using Dal = RentAWorker.DataAccess.Interfaces;
using Entities = RentAWorker.DataAccess.Entities;
using System.Security.Permissions;
using System.ServiceModel.Activation;

namespace RentAWorker.Business.Impl
{
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class CustomerFacade : ICustomerService, IManageCustomerService
    {
        private Dal.ICustomerAccess RegistrationDal;

        public CustomerFacade()
        {
            RegistrationDal = DataAccessFactory.GetCustomerAccess();
        }

        #region ICustomerService Members

        [PrincipalPermission(SecurityAction.Demand, Role="FrontOffice")]
        public int RegisterNewCustomer(Guid membershipId, string companyName)
        {
            Entities.Customer NewCustomer =
                RegistrationDal.CreateCustomer(new Entities.Customer() { MembershipID = membershipId, Name = companyName });
            return NewCustomer.CustomerID;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "FrontOffice")]
        public int GetCustomerForUser(Guid membershipId)
        {
            Entities.Customer Customer = RegistrationDal.GetCustomerByMembershipId(membershipId);
            if (Customer == null)
                return -1;
            else
                return Customer.CustomerID;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "FrontOffice")]
        public RentAWorker.DataAccess.Entities.Customer GetCustomer(int id)
        {
            return RegistrationDal.GetCustomerById(id);
        }

        #endregion

        #region ICustomerManagementService Members

        [PrincipalPermission(SecurityAction.Demand, Role = "AccountManager")]
        public RentAWorker.DataAccess.Entities.Customer[] GetCustomers(string nameSearchString)
        {
            return RegistrationDal.GetCustomers(nameSearchString).ToArray();
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "AccountManager")]
        public void UpdateCustomer(RentAWorker.DataAccess.Entities.Customer customer)
        {
            RegistrationDal.UpdateCustomer(customer);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "AccountManager")]
        public void DeleteCustomer(int customerId)
        {
            RegistrationDal.DeleteCustomer(customerId);
        }

        #endregion
    }
}

